<template>
  <div class="app-container">

    <list-page-container>
      <template slot="table">
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column width="300" label="保单号" align="center" prop="policyNumber"/>
          <el-table-column width="200" label="地块所属人姓名" align="center" prop="name"/>
          <el-table-column width="200" label="地块所属人身份证号" align="center" prop="idCard"/>
          <el-table-column width="200" label="地块序号" align="center" prop="plotNumber"/>
          <el-table-column width="200" label="损失面积" align="center" prop="lossArea"/>
          <el-table-column width="200" label="损失程度" align="center" prop="degreeOfLoss"/>
          <el-table-column label="创建时间" align="center" prop="createTime"/>
          <el-table-column label="修改时间" align="center" prop="updateTime"/>
          <el-table-column label="创建人" align="center" prop="createBy"/>
          <el-table-column label="修改人" align="center" prop="updateBy"/>
          <el-table-column label="是否删除"
                           align="center"
                           prop="deleted"
                           :formatter="deletedFormat"
          />
          <el-table-column fixed="right" width="100" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
              >详情
              </el-button>


            </template>
          </el-table-column>
        </el-table>
      </template>

      <template slot="pageDisplay">
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
      </template>
    </list-page-container>


    <el-dialog :title="title" :visible.sync="open" width="530px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="保单号" prop="policyNumber">
          <el-input v-model="form.policyNumber" placeholder="请输入保单号" disabled/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" disabled/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" disabled/>
        </el-form-item>
        <el-form-item label="地块序号" prop="plotNumber">
          <el-input v-model="form.plotNumber" placeholder="请输入地块序号" disabled/>
        </el-form-item>
        <el-form-item label="损失面积" prop="lossArea">
          <el-input v-model="form.lossArea" placeholder="请输入损失面积" disabled/>
        </el-form-item>
        <el-form-item label="损失程度" prop="degreeOfLoss" >
          <el-select  v-model="form.degreeOfLoss" disabled>
            <el-option
                v-for="damage in damageOptions"
                :key="damage.value"
                :label="damage.label"
                :value="damage.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="照片" prop="photo">

          <el-image
              style="width: 100px; height: 100px"
              :src="form.photo"
              :preview-src-list="form.photoList">
          </el-image>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
        title=""
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <el-upload
          class="upload-demo"
          :action="uploadAction"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="1"
          :on-exceed="handleExceed"
          :on-success="importData"
          :headers="{token}"
          :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>

      </el-upload>
<!--      <span slot="footer" class="dialog-footer">-->
<!--    <el-button @click="dialogVisible = false">取 消</el-button>-->
<!--    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>-->
<!--  </span>-->
    </el-dialog>


  </div>
</template>

<script>

import {
  getClaimsModelList,
  addClaimsModel,
  updateClaimsModel,
  delClaimsModel,
  getClaimsModelById,

  importClaimsModel,

} from "@/api/precisionCompensate";

import {getInspectionList,
  addInspection,
  updateInspection,
  delInspection,
  getInspection,
  uploadUrl,
  getImagePath,
} from "@/api/localeInspection"
import { getToken } from '@/utils/auth'
import {listDept} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import {OPERATE_TYPE} from "@/constantData";

export default {
  dicts: ['enum_user_state', 'enum_user_type'],
  components:{Treeselect},
  name: "LocaleInspection",
  data() {
    return {
      token:'',
      uploadAction : process.env.VUE_APP_BASE_API +uploadUrl,
      plantOptions: [
          {
        value: '10202',
        label: '旱地玉米'
      }, {
        value: '104',
        label: '大豆'
      }, {
        value: '104',
        label: '大豆收入'
      }, {
        value: '103',
        label: '水稻'
      }, {
        value: '10201',
        label: '水地玉米'
      },{
        value: '10202',
        label: '玉米期货'
      },{
        value: '104',
        label: '大豆期货'
      },{
        value: '181',
        label: '水稻制种'
      }],  //作物下拉列表
      damageOptions:[
        {
          value: '未受灾',
          label: '未受灾'
        } ,
        {
          value: '轻度受灾',
          label: '轻度受灾'
        } ,
        {
          value: '中度受灾',
          label: '中度受灾'
        } ,
        {
          value: '重度受灾',
          label: '重度受灾'
        }

      ],  //受灾程度下拉列表
      //条目id
      itemId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      dataList: [],
      total: 0,
      loading: false,
      multiple: true,
      open: false,
      open2: false,
      // 弹出层标题
      title: "",
      form: {},
      baseForm: {
        policyNumber:null,
        name: null,
        idCard: null,
        plotNumber: null,
        lossArea:null,
        degreeOfLoss:null,
        photo:null,

      },
      // 表单校验
      rules: {
        // policyNumber: [
        //   {required: true, message: "保单号不能为空", trigger: "change"}
        // ],
        // name: [
        //   {required: true, message: "姓名不能为空", trigger: "blur"}
        // ],
        // idCard: [
        //   {required: true, message: "身份证号不能为空", trigger: "change"}
        // ],
        // plotNumber: [
        //   {required: true, message: "地块序号不能为空", trigger: "change"}
        // ],
        // lossArea: [
        //   {required: true, message: "损失面积不能为空", trigger: "change"}
        // ],
        // degreeOfLoss: [
        //   {required: true, message: "损失程度不能为空", trigger: "change"}
        // ],
        // idCard: [
        //   {required: true, message: "身份证号不能为空", trigger: "change"}
        // ],

      },

      // 选中数组
      ids: [],
      deptOptions:[],
      hanType:'',
      OPERATE_TYPE,

      dialogVisible: false,  //导入弹框
      fileList:[],//导入数据
      fileId:null,  //文件id

    }
  },

  mounted(){
    this.token = getToken();
  },

  methods: {
    //上传照片成功回调
    handlePictureCardPreview(){
      this.form.photo= file.url;
    },
    //映射是否删除
    deletedFormat(row){

        if(row.deleted === '1'){
          return '是';
        }else if(row.deleted === '0'){
          return '否'
        }else{
          return null
        }

    },
    //打开导入
    openImport(){
      this.fileList=[];
      this.dialogVisible=true;

    },
    //上传文件成功回调
    importData(res){
      if(res && res.code==200){
        if(res.data && res.data.id){
          this.fileId = res.data.id;
          this.sendFileId(res.data.id);
        }
      }
    },
    sendFileId(id){
      importClaimsModel({id}).then(res=>{
        if(res.code==200){
          this.dialogVisible=false;//关闭弹窗
          this.$message.success('导入成功')
          this.fileList=[];
        }else{
          this.dialogVisible=false;//关闭弹窗
          this.$message.error('导入失败');
          this.fileList=[];
        }
      },err=>{
        this.dialogVisible=false;//关闭弹窗
        this.fileList=[];
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.id
      this.hanType = OPERATE_TYPE.UPDATE
      getInspection({id:dictId}).then(response => {
        this.form = response.data;
        if(this.form.photo){
          this.disposeImagePath();
        }else{
          this.open = true;
          this.title = "修改现场验标信息";
        }
      });
    },
    //获取图片路径方法
    disposeImagePath(){
      getImagePath(this.form.photo).then(res => {

        if(res && res.code==200 && res.data){
          this.form.photo = `/fileDown${res.data.fileUrl}`;
          this.form.photoList = [this.form.photo];
          this.open = true;
          this.title = "修改现场验标信息";
        }
      })
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.id || this.ids.join(',');
      this.$modal.confirm('是否确认删除当前数据项？').then(function () {
        return delInspection({ids:dictIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    // 表单重置
    reset() {
      this.form = _.cloneDeep(this.baseForm);
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.hanType = OPERATE_TYPE.ADD
      this.open = true;
      this.title = "添加现场验标信息";
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getInspectionList(this.queryParams).then(response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
      );
    },
    getDeptList(){
      let owner = this
      listDept().then(response => {
        owner.deptOptions = this.handleTree(response.data, "orgCode", 'parentCode');
      });
    }
  },
  created() {
    this.getList();
    this.getDeptList()
  },
}
</script>

<style scoped>

</style>
